package demo8;

import java.util.Stack;

public class StackImplementationQueue {
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;

    //入队
    //入的时候全部放到stack1中
    public void offer(int val) {
        stack1.push(val);
    }

    //出队
    public int poll() {
        if (isEmpty()) {
            return -1;
        }
        if (!stack2.empty()) {
            return stack2.pop();
        } else {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }

    //获得队顶元素
    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        if (!stack2.empty()) {
            return stack2.peek();
        } else {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
            return stack2.peek();
        }
    }

    //判断队列是不是为空
    public boolean isEmpty() {
        return stack1.empty() && stack2.empty();
    }
}
